package models

import (
	"context"
	. "gin-chat/entity"
)

type _CFriendsMgr struct {
	*_BaseMgr
}

// CFriendsMgr open func
func CFriendsMgr() *_CFriendsMgr {
	ctx, cancel := context.WithCancel(context.Background())
	return &_CFriendsMgr{_BaseMgr: &_BaseMgr{DB: db.Table("c_friends"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_CFriendsMgr) GetTableName() string {
	return "c_friends"
}

// Get 获取
func (obj *_CFriendsMgr) Get() (result CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error

	return
}

// Gets 获取批量结果
func (obj *_CFriendsMgr) Gets() (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error

	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取
func (obj *_CFriendsMgr) WithID(id int) Option {
	return optionFunc(func(o *options) { o.query["id"] = id })
}

// WithUserID user_id获取 用户id
func (obj *_CFriendsMgr) WithUserID(userID int) Option {
	return optionFunc(func(o *options) { o.query["user_id"] = userID })
}

// WithFriendID friend_id获取 朋友id
func (obj *_CFriendsMgr) WithFriendID(friendID int) Option {
	return optionFunc(func(o *options) { o.query["friend_id"] = friendID })
}

// GetByOption 功能选项模式获取
func (obj *_CFriendsMgr) GetByOption(opts ...Option) (result CFriends, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error

	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_CFriendsMgr) GetByOptions(opts ...Option) (results []*CFriends, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error

	return
}

//////////////////////////enume case ////////////////////////////////////////////

// GetFromID 通过id获取内容
func (obj *_CFriendsMgr) GetFromID(id int) (result CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}

// GetBatchFromID 批量查找
func (obj *_CFriendsMgr) GetBatchFromID(ids []int) (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` IN (?)", ids).Find(&results).Error

	return
}

// GetFromUserID 通过user_id获取内容 用户id
func (obj *_CFriendsMgr) GetFromUserID(userID int) (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Preload("UserInfo").Find(&results).Error

	return
}

// GetBatchFromUserID 批量查找 用户id
func (obj *_CFriendsMgr) GetBatchFromUserID(userIDs []int) (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` IN (?)", userIDs).Find(&results).Error

	return
}

// GetFromFriendID 通过friend_id获取内容 朋友id
func (obj *_CFriendsMgr) GetFromFriendID(friendID int) (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`friend_id` = ?", friendID).Find(&results).Error

	return
}

// GetBatchFromFriendID 批量查找 朋友id
func (obj *_CFriendsMgr) GetBatchFromFriendID(friendIDs []int) (results []*CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`friend_id` IN (?)", friendIDs).Find(&results).Error

	return
}

//////////////////////////primary index case ////////////////////////////////////////////

// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_CFriendsMgr) FetchByPrimaryKey(id int) (result CFriends, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}
